#include "globals.h"
#include "Target.h"
#include "Utility.h"

// A structure for our custom vertex type
struct CUSTOMVERTEX
{
    FLOAT x, y, z;      // The transformed position for the vertex
    DWORD color;        // The vertex color
};

// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)

void Target::Draw() 
{
	g_engine.getVCard()->SetMaterial(&CUtility::CreateMaterial(1.0f, 0.0f, 0.0f));
	g_engine.getVCard()->SetTexture(0, 0);
	g_engine.getVCard()->SetStreamSource( 0, g_pVB, 0, sizeof( CUSTOMVERTEX ) );
	g_engine.getVCard()->SetFVF( D3DFVF_CUSTOMVERTEX );
	g_engine.getVCard()->DrawPrimitive( D3DPT_LINELIST, 0, 2 );
}

//-----------------------------------------------------------------------------
// Name: Init()
// Desc: Creates a vertex buffer and fills it with our vertices. The vertex
//       buffer is basically just a chuck of memory that holds vertices. After
//       creating it, we must Lock()/Unlock() it to fill it. For indices, D3D
//       also uses index buffers. The special thing about vertex and index
//       buffers is that they can be created in device memory, allowing some
//       cards to process them in hardware, resulting in a dramatic
//       performance gain.
//-----------------------------------------------------------------------------
HRESULT Target::Init()
{
   
   /* // Initialize three vertices for rendering a Grid
	CUSTOMVERTEX vertices[] = {{0.0f, 0.0f, 0.0f, 0xFFFFFF}};

    // Create the vertex buffer. Here we are allocating enough memory
    // (from the default pool) to hold all our 3 custom vertices. We also
    // specify the FVF, so the vertex buffer knows what data it contains.
    if( FAILED( g_engine.getVCard()->CreateVertexBuffer( sizeof( vertices ),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
    {
        return E_FAIL;
    }

    // Now we fill the vertex buffer. To do this, we need to Lock() the VB to
    // gain access to the vertices. This mechanism is required becuase vertex
    // buffers may be in device memory.
    VOID* pVertices;
    if( FAILED( g_pVB->Lock( 0, sizeof( vertices ), ( void** )&pVertices, 0 ) ) )
        return E_FAIL;
    memcpy( pVertices, vertices, sizeof( vertices ) );
    g_pVB->Unlock();

    return S_OK;*/

	CUSTOMVERTEX data[]= {
		{ 0.0f, -0.01f, 0.0f,  D3DCOLOR_COLORVALUE( 1.0, 0.0, 0.0, 1.0 ) },
		{ 0.0f,  0.01f, 0.0f,  D3DCOLOR_COLORVALUE( 0.0, 1.0, 0.0, 1.0 ) },
		{-0.01f,  0.0f, 0.0f,  D3DCOLOR_COLORVALUE( 0.0, 0.0, 1.0, 1.0 ) },
		{ 0.01f,  0.0f, 0.0f,  D3DCOLOR_COLORVALUE( 1.0, 1.0, 0.0, 1.0 ) },
	};

	void *vb_vertices;
	HRESULT hr;

	hr = g_engine.getVCard()->CreateVertexBuffer( sizeof(data),      //Length
												  D3DUSAGE_WRITEONLY,//Usage
												  D3DFVF_CUSTOMVERTEX,           //FVF
												  D3DPOOL_MANAGED,   //Pool
												  &g_pVB,        //ppVertexBuffer
												  NULL);             //Handle
	if(FAILED(hr))
	{
	  return hr;
	}

	hr=g_pVB->Lock(0, //Offset
					  0, //SizeToLock
					  &vb_vertices, //Vertices
					  0);  //Flags
	if(FAILED(hr))
	{
	  return hr;
	}

	memcpy( vb_vertices, data,sizeof(data));

	g_pVB->Unlock();


	return D3D_OK;
}

//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Target::Cleanup()
{
    if( g_pVB != NULL )
        g_pVB->Release();
}